package com.eeprint.web.controller.platform;

import com.eeprint.common.utils.AjaxResult;
import com.eeprint.system.domain.platform.FORM.PlatformAddOrUpdateTenantFORM;
import com.eeprint.system.domain.platform.FORM.PlatformLoginFORM;
import com.eeprint.system.domain.platform.FORM.PlatformUserUpdateFORM;
import com.eeprint.system.domain.platform.PlatformTenant;
import com.eeprint.system.domain.platform.PlatformUser;
import com.eeprint.system.service.platform.PlatformLoginService;
import com.eeprint.system.service.platform.PlatformTenantService;
import com.eeprint.system.service.platform.PlatformUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
  * @Author newhonor
  * @Date 2020/10/18
  * @Version 1.0
**/

@RestController
public class PlatformHandler {

    @Autowired
    private PlatformLoginService platformLoginService;

    @Autowired
    private PlatformUserService platformUserService;

    @Autowired
    private PlatformTenantService platformTenantService;

    @PostMapping("/platform/open/user/login")
    public AjaxResult platformLogin(@RequestBody PlatformLoginFORM platformLoginFORM, HttpSession session){
        PlatformUser platformUser = platformLoginService.platformLogin(platformLoginFORM.getUserPhone(), platformLoginFORM.getUserPassword());
        AjaxResult result = null;
        if(platformUser != null){
            result = AjaxResult.success(platformUser);
            session.setAttribute("platformAdminId",platformUser.getId());
        }else {
            result = AjaxResult.error();
        }
        return result;
    }

    @GetMapping("/platform/auth/store/list")
    public AjaxResult findTenants(@RequestParam("page") int page,@RequestParam("limit") int limit){
//        System.out.println(page+' '+limit);
        List<PlatformTenant> list = platformTenantService.findTenants(page,limit);
        AjaxResult result = null;
        if(list!=null){
            result = AjaxResult.success(list);
        }else {
            result=AjaxResult.error();
        }
        return result;
    }

    @GetMapping("/platform/auth/store/pages")
    public AjaxResult countTenants(@RequestParam("limit") int limit){
        int pages = platformTenantService.countTenants(limit);
        AjaxResult result = null;
        Map<String,Integer> data = new HashMap<>();
        data.put("pages",pages);
        result = AjaxResult.success(data);
        return result;
    }

    @PostMapping("/platform/auth/store/add")
    public AjaxResult addTenant(@RequestBody PlatformAddOrUpdateTenantFORM platformAddOrUpdateTenantFORM, HttpSession session){
        AjaxResult result = null;
        if(platformTenantService.addTenant(platformAddOrUpdateTenantFORM, session)){
            result = AjaxResult.success();
        }else {
            result = AjaxResult.error();
        }
        return result;
    }

    @PostMapping("/platform/auth/store/update")
    public AjaxResult updateTenant(@RequestBody PlatformAddOrUpdateTenantFORM platformAddOrUpdateTenantFORM, HttpSession session){
        AjaxResult result = null;
        if(platformTenantService.updateTenant( platformAddOrUpdateTenantFORM,session)){
            result = AjaxResult.success();
        }else {
            result = AjaxResult.error();
        }

        return result;
    }

    @PostMapping("/platform/auth/user/detail")
    public AjaxResult findPlatformUserById(HttpSession session){
        BigInteger id = (BigInteger)session.getAttribute("platformAdminId");
        PlatformUser platformUser = platformUserService.findPlatformUserById(BigInteger.valueOf(1));//测试
        AjaxResult result=null;
        Map<String,Object> data = new HashMap<>();
        data.put("detail",platformUser);
        data.put("passwordStrength","强");
        if(platformUser!=null){
            result = AjaxResult.success(data);
        }else {
            result=AjaxResult.error();
        }
        return result;
    }

    @PostMapping("/platform/auth/user/update")
    public AjaxResult updatePlatformUserPassword(@RequestBody PlatformUserUpdateFORM platformUserUpdateFORM){
        AjaxResult result=null;
        if(platformUserService.updatePlatformUserPassword(platformUserUpdateFORM)){
            result = AjaxResult.success();
        }else {
            result=AjaxResult.error();
        }

        return result;
    }

}
